Skip to content

[release/11.0] Don't cache truncated/aborted responses in OutputCacheMiddleware - #68773

Merged
wtgodbe merged 1 commit into
dotnet:release/11.0from
DeagleGross:deaglegross-backport-output-cache-rel-11
Aug 25, 2026
Merged

[release/11.0] Don't cache truncated/aborted responses in OutputCacheMiddleware#68773
wtgodbe merged 1 commit into
dotnet:release/11.0from
DeagleGross:deaglegross-backport-output-cache-rel-11

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

Backport of #68683 to release/11.0

Don't cache truncated/aborted responses in OutputCacheMiddleware

Description

Prevents OutputCacheMiddleware from storing responses after the request is aborted, and only shares a response with requests waiting on the same cache key when the response was actually cached. Waiters now re-execute the pipeline instead of receiving a truncated or aborted response.

Fixes #66877
Fixes #56427

Customer Impact

Without this fix, an aborted request can cache a partial response and serve it to subsequent clients until the cache entry expires. Requests waiting through output-cache locking can also receive a truncated in-flight response.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

The change is isolated to output-cache response finalization and pending-request reuse, with targeted automated coverage for aborted and truncated responses.

Verification

  • Manual (required)
  • Automated

Microsoft.AspNetCore.OutputCaching.Tests passes on net11.0 with native builds disabled.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

When servicing release/2.3

  • Make necessary changes in eng/PatchConfig.props

…otnet#68683)

OutputCacheMiddleware could store a response whose body was cut short,
and share that entry with requests waiting on the same cache key.

FinalizeCacheBodyAsync now skips storage when the request was aborted and
reports whether the response was cached. The caller releases the pending
entry unless it was actually stored, so waiters re-execute instead of
receiving a truncated body.

Fixes dotnet#66877
Copilot AI lite review requested due to automatic review settings August 25, 2026 10:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR backports a fix to the OutputCacheMiddleware locking/finalization path to avoid caching (and reusing for lock waiters) responses that are truncated or produced after the request is aborted, preventing partial responses from being served to other clients.

Changes:

  • Updates OutputCacheMiddleware response finalization to return whether the response was actually cached, and uses that to decide whether a pending/locking entry may be reused.
  • Prevents caching when HttpContext.RequestAborted is already canceled at cache-body finalization time.
  • Adds/extends unit tests covering aborted responses and truncated responses under locking.
Show a summary per file
File Description
src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs Makes cache-body finalization report cache success and prevents caching when the request is already aborted; releases pending entries when not cached.
src/Middleware/OutputCaching/test/OutputCacheMiddlewareTests.cs Adds tests for “don’t cache on aborted request” and for ignoring truncated responses under locking.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +1184 to +1188
[Fact]
public async Task AbortedRequest_IsNotServedToSubsequentRequests()
{
var responseCounter = 0;
var cache = GetStore();
Comment on lines 447 to 451
await OutputCacheEntryFormatter.StoreAsync(context.CacheKey, context.CachedResponse, context.Tags, context.CachedResponseValidFor,
_store, _logger, context.HttpContext.RequestAborted);

return true;
}
@wtgodbe wtgodbe added the Servicing-approved Shiproom has approved the issue label Aug 25, 2026
@wtgodbe

wtgodbe commented Aug 25, 2026

Copy link
Copy Markdown
Member

Approved over email

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @DeagleGross. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@wtgodbe
wtgodbe merged commit b7597c9 into dotnet:release/11.0 Aug 25, 2026
31 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc2 milestone Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants